package smetana.modules.business;

import org.apache.pivot.collections.List;
import smetana.domain.join.DocReport;
import smetana.mapper.join.ReportMapper;
import smetana.domain.raw.Doc;
import smetana.mapper.raw.DocMapper;
import smetana.modules.business.meta.Commit;
import smetana.util.Pivot;

/**
 * @author dmitry.mamonov
 */
class BusinessDocImpl extends BaseImpl implements BusinessDoc {
    @Override
    @Commit
    public Doc newDoc() {
        DocMapper mapper = sql.getMapper(DocMapper.class);
        Doc doc = new Doc(mapper.nextId());
        mapper.insert(doc);
        return mapper.findById(doc.getId());
    }


    @Override
    public Doc findDocById(Integer id) {
        if (id == null) {
            return null;
        } else {
            DocMapper mapper = sql.getMapper(DocMapper.class);
            Doc document = mapper.findById(id);
            return document;
        }
    }

    @Override
    public DocReport findDocReportById(Integer id) {
        if (id == null) {
            return null;
        } else {
            ReportMapper mapper = sql.getMapper(ReportMapper.class);
            DocReport docReport = mapper.findDocReportById(id);
            return docReport;
        }
    }


    @Override
    @Commit
    public void saveDoc(Doc document) {
        DocMapper mapper = sql.getMapper(DocMapper.class);
        mapper.update(document);
    }

    @Override
    @Commit
    public void deleteDoc(Doc doc) {
        DocMapper mapper = sql.getMapper(DocMapper.class);
        mapper.delete(doc.getId());
    }

    @Override
    public java.util.List<Doc> listDocs() {
        DocMapper mapper = sql.getMapper(DocMapper.class);
        return mapper.listAll();
    }

    @Override
    public java.util.List<Doc> listDocsWithDeleted() {
        DocMapper mapper = sql.getMapper(DocMapper.class);
        return mapper.listAllWithDeleted();
    }

    @Override
    public java.util.List<DocReport> listDocReports() {
        ReportMapper mapper = sql.getMapper(ReportMapper.class);
        return mapper.listDocReports();
    }

    @Override
    public List<DocReport> pivotListDocReports() {
        return Pivot.fromList(listDocReports());
    }
}
